﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpSteppedViewExternal.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "GbmpModelViewExternalDataId.h"
#include "GbmpModelViewUtil.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElementParameters.h"
#include "IElementParentReporter.h"
#include "IExternalDataComponent.h"
#include "ILevel.h"
#include "IModelView.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IWorkPlane.h"
#include "ModelViewParameterValidator.h"

#include "IViewClipRange.h"
#include "GbmpModelViewInput.h"
#include "GbmpCalculatorBase.h"
#include "IGenericElement.h"
#include "ICalculatorCollection.h"
#include "RegenDataSchema.h"
#include "IRegenerator.h"
#include "IRegeneratorDataIdCreator.h"
#include "ISteppedViewSymbol.h"
#include "LevelUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

DEFINE_MODEL_VIEW_BASE_METHODS(GbmpSteppedViewExternal)

DBOBJECT_DATA_DEFINE(GbmpSteppedViewExternal)
{
    m_pOwnerElement = nullptr;
}
namespace gcmp
{
    class GbmpStairViewCoordCalculator : public GbmpCalculatorBase
    {
        DECLARE_CALCULATOR(GbmpStairViewCoordCalculator, IModelView)
    public:
        GbmpStairViewCoordCalculator(IDocument* pDoc, const RegenDataId& outputDataId) :GbmpCalculatorBase(pDoc, outputDataId) {}
        virtual void ReportInputDataIds(std::vector<RegenDataId> & oInputDatas) const override;
        virtual void Execute() override;
    };

    void GbmpStairViewCoordCalculator::ReportInputDataIds(std::vector<RegenDataId> & dataIds) const
    {
        const IModelView* pModelView = GetTarget();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pModelView, L"所属视图为空",L"GDMPLab",L"2024-03-30");
        
        const IDocument* pDoc = GetDocument();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"所属文档为空",L"GDMPLab",L"2024-03-30");

        const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pDoc->GetElement(pModelView->GetViewSymbolId()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"所属文档为空",L"GDMPLab",L"2024-03-30");

    }

    void GbmpStairViewCoordCalculator::Execute()
    {
    }
}

IMPLEMENT_CALCULATOR_CREATOR(gcmp, GbmpStairViewCoordCalculator);

GbmpSteppedViewExternal* GbmpSteppedViewExternal::Create(const GbmpModelViewInput& input, const ElementId& idModelView, const SteppedLineStruct& stStairLine)
{
    return nullptr;
}

GbmpSteppedViewExternal* GbmpSteppedViewExternal::Get(IModelView* pModelView)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    if (IExternalDataComponent * pExternalDataComponent = pModelView->GetExternalDataComponent())
    {
        IExternalData* pData = pExternalDataComponent->FindExternalData(MODEL_VIEW_EXTERNAL_DATA_ID);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");
        return quick_cast<GbmpSteppedViewExternal>(pData);
    }
    return nullptr;
}

const GbmpSteppedViewExternal* GbmpSteppedViewExternal::Get(const IModelView* pModelView)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    if (const IExternalDataComponent * pExternalDataComponent = pModelView->GetExternalDataComponent())
    {
        const IExternalData* pData = pExternalDataComponent->FindExternalData(MODEL_VIEW_EXTERNAL_DATA_ID);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pData, L"pData为空",L"GDMPLab",L"2024-03-30");
        return quick_cast<const GbmpSteppedViewExternal>(pData);
    }
    return nullptr;
}

GbmpSteppedViewExternal* GbmpSteppedViewExternal::RegisterExternalData(IModelView* pModelView)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<GbmpSteppedViewExternal> opSectionView = NEW_AS_OWNER_PTR(GbmpSteppedViewExternal);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSectionView, L"opSectionView为空",L"GDMPLab",L"2024-03-30");

    opSectionView->SetOwnerElement(pModelView);

    GbmpSteppedViewExternal* pSectionView = opSectionView.get();
    if (IExternalDataComponent * pExternalDataComponent = pModelView->GetExternalDataComponent())
    {
        pExternalDataComponent->RegisterExternalData(MODEL_VIEW_EXTERNAL_DATA_ID, TransferOwnership(opSectionView));
        return pSectionView;
    }
    return nullptr;
}

void GbmpSteppedViewExternal::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    auto it = deletedElementIds.find(GetWorkLevelId__());
    if (it != deletedElementIds.end())
    {
        ElementId nextWorkLevelId = GbmpModelViewUtil::GetSectionViewNextWorkLevel(GetDocument(), *it);
        DBG_WARN_AND_RETURN_VOID_UNLESS(nextWorkLevelId.IsValid(), L"nextWorkLevelId无效",L"GDMPLab",L"2024-03-30");
        SetWorkLevelId(nextWorkLevelId);
    }

}

void GbmpSteppedViewExternal::ReportParents(IElementParentReporter& reporter) const
{
    reporter.ReportWeak(GetWorkLevelId());
}

void GbmpSteppedViewExternal::GetCalculators(ICalculatorCollection * calculators) const
{
    //ADD_CALCULATOR(GbmpStairViewCoordCalculator, GetDocument(), GetSectionViewCoordRdId());
}

void GbmpSteppedViewExternal::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds, L"pParamDefIds为空",L"GDMPLab",L"2024-03-30");
    pParamDefIds->push_back(PARAMETER_ID(GbmpViewWorkLevelBuiltInParameter));
}

OwnerPtr<IParameter> GbmpSteppedViewExternal::GetNativeParameter(int paramDefId) const
{
    if (paramDefId == PARAMETER_ID(GbmpViewWorkLevelBuiltInParameter))
    {
        ParameterAttributes pa = {false, false, true};
        OwnerPtr<IParameter> pParam = IParameter::CreateParameter(GetDocument(), 
            ParameterStorageType::ElementId, pa, PARAMETER_UID(GbmpViewWorkLevelBuiltInParameter), 
            ParameterProcessType::GeneralInput);
        pParam->SetValueAsElementId(GetWorkLevelId());
        return TransferOwnership(pParam);
    }
    return nullptr;
}

bool GbmpSteppedViewExternal::SetNativeParameter(const IParameter *param, std::wstring* errorMsg/* = nullptr*/)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (param->GetParameterDefinitionId() == PARAMETER_ID(GbmpViewWorkLevelBuiltInParameter))
    {
        SetWorkLevelId(param->GetValueAsElementId());
        return true;
    }
    return false;
}

bool GbmpSteppedViewExternal::IsParameterModifiable(int paramDefId) const
{
    return true;
}

void GbmpSteppedViewExternal::Report(const ICopyContext& copyContext, ICopyStrategyReporter& reporter) const
{
    reporter.ReportToCopy(GetOwnerElementId());
    reporter.ReportToKeep(GetWorkLevelId());
}

IDocument* GbmpSteppedViewExternal::GetDocument() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetDocument() : nullptr;
}

ElementId GbmpSteppedViewExternal::GetOwnerElementId() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetElementId() : ElementId::InvalidID;
}

NdbObject* GbmpSteppedViewExternal::GetTopOwnerObject() const
{
    if (!GetDocument())
        return nullptr;

    return quick_cast<NdbObject>(GetDocument()->GetElement(GetOwnerElementId()));
}

bool GbmpSteppedViewExternal::SetWorkLevelId(ElementId workLevelId)
{
    SetWorkLevelId__(workLevelId);

    return true;
}
